package org.lebutch.sw.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.io.Serializable;
import java.util.List;

import org.lebutch.sw.Constants;
import org.lebutch.sw.R;
import org.lebutch.sw.SevenWondersApp;

import wonder.Board;
import wonder.BoardInitialResource;
import wonder.ISWService;
import wonder.SWServiceFactory;

public class ResultActivity extends Activity {
    private SevenWondersApp app;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);

        app = (SevenWondersApp)getApplication();

        int pc = app.getCurrentPlayersCount();
        try {
            ISWService manager = SWServiceFactory.getInstance().getSWService();
            final List<Board> game = manager.randomSetup(pc, app.getAvailableSets());

            ListView listView = (ListView)findViewById(R.id.playersList);
            listView.setAdapter(new WonderListAdapter(this, game));

            listView.setOnItemClickListener(new ListView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> av, View view, int pos, long id) {
                    Board board = game.get(pos);
                    Intent intent = new Intent(ResultActivity.this, BoardActivity.class);
                    intent.putExtra(BoardActivity.BOARD_ID, (Serializable)board);
                    startActivity(intent);
                }
            });
        } catch (IllegalArgumentException e) {
            onException();
        } catch (Exception e) {
            Log.e(Constants.LOG_TAG, "Exception using ISWService", e);
        }
    }

    private void onException() {
        AlertDialog.Builder builder;
        LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(
                LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.random_error,
                (ViewGroup)findViewById(R.id.random_error_dialog));

        builder = new AlertDialog.Builder(this)
                .setTitle(R.string.error)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setView(layout)
                .setPositiveButton(getString(android.R.string.ok),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        });
        builder.create().show();
    }

    private class WonderListAdapter extends ArrayAdapter<Board> {
        private List<Board> game;
        private Context context;
        private static final int listViewResourceId = R.layout.icon_list_item_2;
        private static final int imageViewResourceId = R.id.image1;
        private static final int textViewResourceId = android.R.id.text1;
        private static final int detailViewResourceId = android.R.id.text2;

        public WonderListAdapter(Context context, List<Board> objects) {
            super(context, listViewResourceId, textViewResourceId, objects);
            game = objects;
            this.context = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Board board = game.get(position);
            View rowView = convertView;
            if (rowView == null) {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                rowView = inflater.inflate(listViewResourceId, null);
            }

            TextView tv = (TextView)rowView.findViewById(textViewResourceId);
            String text = String.format(getResources().getString(R.string.result_1), 1 + position,
                    board.getName(), board.getSide());
            tv.setText(text);

            tv = (TextView)rowView.findViewById(detailViewResourceId);
            text = getResources().getQuantityString(R.plurals.numberOfSteps, board.getSteps(),
                    board.getSteps(), board.getSet());
            tv.setText(text);

            BoardInitialResource ir = board.getInitialResource();
            if (ir != null) {
                Drawable drw = app.getResourceDrawable(context, ir.getResourceType());
                if (drw != null) {
                    ImageView iv = (ImageView)rowView.findViewById(imageViewResourceId);
                    iv.setImageDrawable(drw);
                    iv.setMaxHeight(drw.getIntrinsicHeight());
                }
            }

            return rowView;
        }
    }
}
